﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using System.Collections;
using FrienDev.Data;

public partial class Controls_Users_ProfileDetailInformationEdit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitDDL();
            if (!UserRules.Instance.UserDetailInfoExisted(UserRules.Instance.GetCurrentUserId()))
            {
                InitUserDetailInfo();
            }
        }
        BindEvent();
    }

    private void BindEvent()
    {
        ddl_Province.SelectedIndexChanged += new EventHandler(ddl_Province_SelectedIndexChanged);
        bt_Save.Click += new EventHandler(bt_Save_Click);
    }

    private void InitUserDetailInfo()
    {
        User user = UserRules.Instance.GetCurrentUser();
        ddl_EduBackground.SelectedValue = user.UsersDetail.EduBackground.ToString();
        ddl_WorkExperience.SelectedValue = user.UsersDetail.WorkExperience.ToString();
        txt_CompanyName.Text = user.UsersDetail.CompanyName.ToString();
        ddl_CompanyType.SelectedValue = user.UsersDetail.CompanyTypeId.ToString();
        ddl_IndustryType.SelectedValue = user.UsersDetail.IndustryTypeId.ToString();
        ddl_CompanyScale.SelectedValue = user.UsersDetail.CompanyScale.ToString();
        txt_CompanyAddress.Text = user.UsersDetail.CompanyAddress.ToString();
        txt_Department.Text = user.UsersDetail.Department.ToString();
        ddl_Title.SelectedValue = user.UsersDetail.TitleId.ToString();
        if (string.IsNullOrEmpty(user.CityId.ToString()))
        {
            ddl_Province.SelectedValue = "1";
            InitDDLCity();
            ddl_City.SelectedValue = "1";
        }
        else
        {

            ddl_Province.SelectedValue = UserRules.Instance.GetProvinceByCityId(Convert.ToInt32(user.CityId.ToString())).ProvinceId.ToString();
            InitDDLCity();
            ddl_City.SelectedValue = user.CityId.ToString();
        }
        txt_PostCode.Text = user.UsersDetail.PostCode.ToString();
        txt_Blog.Text = user.UsersDetail.BlogAddress.ToString();
    }

    void bt_Save_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        if (UserRules.Instance.UserDetailInfoExisted(UserRules.Instance.GetCurrentUserId()))
        {
            UserRules.Instance.InsertUserDetailInfo(Convert.ToInt32(ddl_EduBackground.SelectedValue), Convert.ToInt32(ddl_WorkExperience.SelectedValue), txt_CompanyName.Text,
                Convert.ToInt32(ddl_CompanyType.SelectedValue), Convert.ToInt32(ddl_IndustryType.SelectedValue), Convert.ToInt32(ddl_CompanyScale.SelectedValue), txt_CompanyAddress.Text, txt_Department.Text, Convert.ToInt32(ddl_Title.SelectedValue),
                txt_PostCode.Text.Trim(), Convert.ToInt32(ddl_City.SelectedValue), txt_Blog.Text.Trim());
        }
        else
        {
            UserRules.Instance.UpdateUserDetailInfo(Convert.ToInt32(ddl_EduBackground.SelectedValue), Convert.ToInt32(ddl_WorkExperience.SelectedValue), txt_CompanyName.Text,
                Convert.ToInt32(ddl_CompanyType.SelectedValue), Convert.ToInt32(ddl_IndustryType.SelectedValue), Convert.ToInt32(ddl_CompanyScale.SelectedValue), txt_CompanyAddress.Text, txt_Department.Text, Convert.ToInt32(ddl_Title.SelectedValue),
                txt_PostCode.Text.Trim(), Convert.ToInt32(ddl_City.SelectedValue), txt_Blog.Text.Trim());
        }
        this.up_UserDetailInfoForm.Update();
    }

    void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitDDLCity();
    }

    #region InitDDL
    private void InitDDL()
    {
        InitDDLEduBackground();
        InitDDLWorkExperience();
        InitDDLCompanyType();
        InitDDLIndustryType();
        InitDDLCompanyScale();
        InitDDLTitle();
        InitDDLProvince();
        InitDDLCity();
    }

    private void InitDDLEduBackground()
    {
        ddl_EduBackground.DataSource = DictionaryRules.Instance.GetObjectTypes("EduBackground");
        ddl_EduBackground.DataTextField = "ObjectValue";
        ddl_EduBackground.DataValueField = "DictionaryId";
        ddl_EduBackground.DataBind();
    }

    private void InitDDLWorkExperience()
    {
        ddl_WorkExperience.DataSource = DictionaryRules.Instance.GetObjectTypes("WorkExperience");
        ddl_WorkExperience.DataTextField = "ObjectValue";
        ddl_WorkExperience.DataValueField = "DictionaryId";
        ddl_WorkExperience.DataBind();
    }

    private void InitDDLCompanyScale()
    {
        ddl_CompanyScale.DataSource = DictionaryRules.Instance.GetObjectTypes("CompanyScale");
        ddl_CompanyScale.DataTextField = "ObjectValue";
        ddl_CompanyScale.DataValueField = "DictionaryId";
        ddl_CompanyScale.DataBind();
    }

    private void InitDDLCompanyType()
    {
        IEnumerable<Dictionary> dictionary = DictionaryRules.Instance.GetObjectTypes("Company");
        ddl_CompanyType.DataSource = dictionary;
        ddl_CompanyType.DataTextField = "ObjectValue";
        ddl_CompanyType.DataValueField = "DictionaryId";
        ddl_CompanyType.DataBind();
    }

    private void InitDDLIndustryType()
    {
        ddl_IndustryType.DataSource = DictionaryRules.Instance.GetObjectTypes("Industry");
        ddl_IndustryType.DataTextField = "ObjectValue";
        ddl_IndustryType.DataValueField = "DictionaryId";
        ddl_IndustryType.DataBind();
    }

    private void InitDDLTitle()
    {
        ddl_Title.DataSource = DictionaryRules.Instance.GetObjectTypes("Title");
        ddl_Title.DataTextField = "ObjectValue";
        ddl_Title.DataValueField = "DictionaryId";
        ddl_Title.DataBind();
    }

    private void InitDDLProvince()
    {
        ddl_Province.DataSource = UserRules.Instance.GetProvinces();
        ddl_Province.DataTextField = "ProvinceName";
        ddl_Province.DataValueField = "ProvinceId";
        ddl_Province.DataBind();
    }
    private void InitDDLCity()
    {
        ddl_City.DataSource = UserRules.Instance.GetCitiesByProvinceId(Convert.ToInt32(ddl_Province.SelectedValue.ToString()));
        ddl_City.DataTextField = "CityName";
        ddl_City.DataValueField = "CityId";
        ddl_City.DataBind();
    }
    #endregion

}
